package com.hknc.btscale.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hknc.btscale.R;
import com.hknc.btscale.bean.GoodsBean;
import com.hknc.btscale.bean.SaveGoods;
import com.hknc.btscale.utils.DataConvertUtils;

import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.MyViewHolder> {
    private Context mContext;
    private int resourceId;


    private List<SaveGoods> mGoodsBeans;
    private static final String TAG = "GoodsAdapter";


    public List<SaveGoods> getGoodsBeans() {
        return mGoodsBeans;
    }

    public void setGoodsBeans(List<SaveGoods> goodsBeans) {
        mGoodsBeans = goodsBeans;
    }

    public GoodsAdapter(Context context, int resourceId) {
        mContext = context;
        this.resourceId = resourceId;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(resourceId, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.goodsname.setText(mGoodsBeans.get(position).getGoodsName());
        Glide.with(mContext)
                .load(mGoodsBeans.get(position).getGoodsImg())
                .into(holder.goodsimg);

        String str = "";
        switch (mGoodsBeans.get(position).getUnit()) {
            case "1":
                str = "Weight";
                break;
            case "2":
                str = "件";
                break;
            case "3":
            case "4":
                str = "斤";
                break;
            case "5":
                str = "ton";
                break;
            case "6":
                str = "lb";
                break;
            case "7":
                str = "斤";
                break;
            case "8":
                str = "100g";
                break;
            case "20":
                str = "只";
                break;
            case "21":
                str = "盒";
                break;
            case "22":
                str = "包";
                break;
            case "23":
                str = "箱";
                break;
            case "24":
                str = "片";
                break;
            case "25":
                str = "份";
                break;
            case "26":
                str = "叠";
                break;
            case "27":
                str = "块";
                break;
            case "28":
                str = "套";
                break;
            case "29":
                str = "组";
                break;
            case "30":
                str = "个";
                break;
            case "31":
                str = "条";
                break;
            case "32":
                str = "瓶";
                break;
        }
        holder.unitTv.setText("元/"+str);

        if (mGoodsBeans.get(position).getUnit().equals("3")) {
            String tempPrice = DataConvertUtils.intToStr_2(Double.parseDouble(mGoodsBeans.get(position).getGoodsPrice()) / 200.00);
            //12.00
            String price1 = tempPrice.substring(0, tempPrice.indexOf("."));
            String price2 = tempPrice.substring(tempPrice.indexOf("."));
            holder.goodsprice.setText(price1);
            holder.goodsprice1.setText(price2);
        }else {
            String tempPrice = DataConvertUtils.intToStr_2(Double.parseDouble(mGoodsBeans.get(position).getGoodsPrice()) / 100.00);
            //12.00
            String price1 = tempPrice.substring(0, tempPrice.indexOf("."));
            String price2 = tempPrice.substring(tempPrice.indexOf("."));
            holder.goodsprice.setText(price1);
            holder.goodsprice1.setText(price2);
        }

    }

    @Override
    public int getItemCount() {
        return mGoodsBeans.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView goodsname, goodsprice, goodsprice1, unitTv;
        ImageView goodsimg;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsname = itemView.findViewById(R.id.goodsname_tv);
            goodsprice = itemView.findViewById(R.id.goodsprice_tv);
            goodsimg = itemView.findViewById(R.id.goodsimg_img);
            goodsprice1 = itemView.findViewById(R.id.goodsprice1_tv);
            unitTv = itemView.findViewById(R.id.textView20);
        }
    }
}
